package borrower

import (
	"NovelBook/logic/book"
	"NovelBook/models/res"
	"NovelBook/utils/auth"
	"NovelBook/utils/response"
	"net/http"

	"github.com/gin-gonic/gin"
)

func ReturnBooks(c *gin.Context) {
	idAny, exist := c.Get(auth.UserIDKey)
	id := idAny.(uint)
	if !exist || id < 1 {
		response.Fail(c, http.StatusUnauthorized, response.CodeTokenInvalid)
		return
	}

	var req res.BookUniqueID

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Fail(c, http.StatusBadRequest, response.CodeParamsInvalid)
		return
	}

	// 还书操作
	fine, err := book.ReturnBooksCheck(id, req.BookUniqueIDs)
	switch err != nil {
	case true:
		response.FailWithMsg(c, http.StatusInternalServerError, response.CodeSystemBusy, err.Error())
		return
	}

	response.Success(c, fine)
}
